﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Data;

namespace NLite.Data.Test
{
    public class TestDb
    {
        public class Region : Table
        {
            public SqlInt32 Id = new SqlInt32("RegionId", "Region");
            public SqlString Description = new SqlString("RegionDescription", "Region");
        }

        public class Product:Table
        {
            public Product() : base("Products") { }
            public SqlInt32 UnitPrice = new SqlInt32("UnitPrice", "Products");
            public SqlInt32 UnitsInStock = new SqlInt32("UnitsInStock", "Products");
        }
        //public static _Product Product = new _Product();

        public class Order : Table
        {
            public SqlInt32 Id = new SqlInt32("OrderId", "Orders");
            public SqlInt32 CustomerId = new SqlInt32("CustomerId", "Orders");
            public SqlInt32 EmployeeId = new SqlInt32("EmployeeId", "Orders");

            public Order() : base("Orders") { }
        }
        //public static readonly _Order Order = new _Order();

        //public static readonly _OrderDetail OrderDetail = new _OrderDetail();
        public class OrderDetail : Table
        {
            public OrderDetail() : base("Order Details") { }

            public SqlInt32 OrderId = new SqlInt32("OrderId", "Order Details");
            public SqlInt32 ProductId = new SqlInt32("ProductId", "Order Details");
        }


        public class Type : Table
        {
            public static SqlByte Byte = new SqlByte("Byte", "Type");
            public static SqlInt16 Int16 = new SqlInt16("Int16", "Type");
            public static SqlInt32 Int32 = new SqlInt32("Int32", "Type");
            public static SqlInt64 Int64 = new SqlInt64("Int64", "Type");
            //public static UInt16Column UInt16 = new UInt16Column("UInt16", "Type");
            //public static UInt32Column UInt32 = new UInt32Column("UInt32", "Type");
            //public static UInt64Column UInt64 = new UInt64Column("UInt64", "Type");
            public static SqlDouble Double = new SqlDouble("Double", "Type");
            public static SqlSingle Single = new SqlSingle("Single", "Type");
            public static SqlDecimal Decimal = new SqlDecimal("Decimal", "Type");

            public static SqlBoolean Boolean = new SqlBoolean("Boolean", "Type");
            public static SqlString String = new SqlString("String", "Type");
            public static SqlAnsiString AnsiString = new SqlAnsiString("AnsiString", "Type");
            public static SqlClob Clob = new SqlClob("Clob", "Type");
            public static SqlNClob NClob = new SqlNClob("NClob", "Type");

            public static SqlGuid Guid = new SqlGuid("Guid", "Type");
            public static SqlDateTime DateTime = new SqlDateTime("DateTime", "Type");
            public static SqlBinary Binary = new SqlBinary("Binary", "Type");
        }

        public class Type2 : Table
        {
            public static SqlByte Byte = new SqlByte("Byte", "Type2");
            public static SqlInt16 Int16 = new SqlInt16("Int16", "Type2");
            public static SqlInt32 Int32 = new SqlInt32("Int32", "Type2");
            public static SqlInt64 Int64 = new SqlInt64("Int64", "Type2");
            //public static UInt16Column UInt16 = new UInt16Column("UInt16", "Type");
            //public static UInt32Column UInt32 = new UInt32Column("UInt32", "Type");
            //public static UInt64Column UInt64 = new UInt64Column("UInt64", "Type");
            public static SqlDouble Double = new SqlDouble("Double", "Type2");
            public static SqlSingle Single = new SqlSingle("Single", "Type2");
            public static SqlDecimal Decimal = new SqlDecimal("Decimal", "Type2");

            public static SqlBoolean Boolean = new SqlBoolean("Boolean", "Type2");
            public static SqlString String = new SqlString("String", "Type2");
            public static SqlGuid Guid = new SqlGuid("Guid", "Type2");
            public static SqlDateTime DateTime = new SqlDateTime("DateTime", "Type2");
            public static SqlBinary Binary = new SqlBinary("Binary", "Type2");
        }

        public static readonly Customer _Customer = new Customer();
        public class Customer : Table
        {
            public Customer() : base("Customers") { }

            public SqlString Id = new SqlString("CustomerId", "Customers");
            public SqlInt32 RegionId = new SqlInt32("RegionId", "Customers");
            public SqlString ContactTitle = new SqlString("ContactTitle", "Customers");
        }
    }

    [Serializable]
    public abstract class EntityMapping : Table
    {
        public EntityMapping(string name) : base(name, null) { Init(); }
        public EntityMapping(string name, string alias) : base(name, alias) { Init(); }
        public EntityMapping() { Init(); }

        public SqlInt64 Id { get; private set; }

        protected virtual void Init()
        {
            Id = CreateColumn<SqlInt64>("ID");
        }
    }

    [Serializable]
    public class NamedEntityMapping : EntityMapping
    {
        public NamedEntityMapping(string name) : base(name, null) { }
        public NamedEntityMapping(string name, string alias) : base(name, alias) { }
        public NamedEntityMapping() { }
        public SqlString Name { get; protected set; }
    }

    [Serializable]
    public class AccessLogMapping : EntityMapping
    {
        public AccessLogMapping() : base("CMATRIX_USERACCESSLOG") { }
        public SqlDateTime AccessTime { get; private set; }
        public virtual SqlInt64 EmployeeID { get; private set; }
        public virtual SqlString AccessType { get; private set; }

        protected override void Init()
        {
            base.Init();
            AccessTime = CreateColumn<SqlDateTime>("ACCESS_TIME");
            EmployeeID = CreateColumn<SqlInt64>("EMPLOYEE_ID");
            AccessType = CreateColumn<SqlString>("ACCESS_TYPE");
        }
    }

    public static class MappingContext
    {
        public static IQuery<AccessLogMapping> AccessLog
        {
            get
            {
                return SqlManager.From<AccessLogMapping>();
            }
        }
    }
}
